package com.Jiang.service;

import com.Jiang.dao.ShopCar.ShopCarMapper;
import com.Jiang.pojo.ShopCar;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("ShopCarServiceImpl")
public class ShopCarServiceImpl implements ShopCarService {
    @Autowired
    ShopCarMapper shopCarMapper;

    public void setShopCarMapper(ShopCarMapper shopCarMapper) {
        this.shopCarMapper = shopCarMapper;
    }

    //    1.增加购物车
    @Override
    public int addShopCar(Map map) {
//        List<ShopCar> shopCars = shopCarMapper.selectShopCarByUserId(shopCar.getUserId()); //根据userId查询所选的书籍
//        for (ShopCar car : shopCars) {
//            if (car.getBookId() == shopCar.getBookId()) {
//                int current = car.getNum();
//                current += shopCar.getNum();
//                car.setNum(current);
//                return 0;
//            }
//        }
//        return shopCarMapper.addShopCar(shopCar);
//    }
        String userId = (String) map.get("userId");
        int bookid = (int) map.get("bookId");
        List<ShopCar> shopCars = shopCarMapper.selectShopCarByUserId(userId); //根据userId查询所选的书籍
        for (ShopCar car : shopCars) {
            if (car.getBookId() == bookid) {
                int current = car.getNum();
                current += 1;
                car.setNum(current);
//                HashMap m = new HashMap();
//                m.put("userId",)
//                map.put("num",current);
//                shopCarMapper.updateShopCar(map);
                shopCarMapper.updateByCar(car);
                return 0;
            }
        }
        return shopCarMapper.addShopCar(map);
    }

    //2.用户删除购物车中的信息
    @Override
    public int deleteShopCar(Map map) {
        return shopCarMapper.deleteShopCar(map);
    }

    //3.更改用户购物车中的数量
    @Override
    public int updateShopCar(Map map) {
        return shopCarMapper.updateShopCar(map);
    }

    //4.查询用户购物车中的物品
    @Override
    public List<ShopCar> selectShopCarByUserId(String userId) {
        return shopCarMapper.selectShopCarByUserId(userId);

    }

    //更新购物车数据数量
    @Override
    public int updateByCar(ShopCar shopCar) {
        return shopCarMapper.updateByCar(shopCar);
    }

    //商家查询全部购物车
    @Override
    public List<ShopCar> queryCars(Map map) {
        return shopCarMapper.queryCars(map);
    }

    @Override
    public int deleteAll(String userId) {
        return shopCarMapper.deleteAll(userId);
    }

    @Override
    public int subNum(Map map) {
        return shopCarMapper.subNum(map);
    }

    @Override
    public int addNum(Map map) {
        return shopCarMapper.addNum(map);
    }

    @Override
    public int toCheck(Map map) {
        return shopCarMapper.toCheck(map);
    }

    @Override
    public List<ShopCar> checkedShopCar(String userId) {
        return shopCarMapper.checkedShopCar(userId);
    }

    @Override
    public int deleteShopCarByOrder(String userId) {
        return shopCarMapper.deleteShopCarByOrder(userId);
    }
}
